import os

import face_recognition


# 人脸差异比对 默认0.6  差异越小 越相同
# Often instead of just checking if two faces match or not (True or False), it's helpful to see how similar they are.
# You can do that by using the face_distance function.

# The model was trained in a way that faces with a distance of 0.6 or less should be a match. But if you want to
# be more strict, you can look for a smaller face distance. For example, using a 0.55 cutoff would reduce false
# positive matches at the risk of more false negatives.

# Note: This isn't exactly the same as a "percent match". The scale isn't linear. But you can assume that images with a
# smaller distance are more similar to each other than ones with a larger distance.

# Load some images to compare against
def load_data():
    list_data = os.listdir('./pic')
    known_encodings = []
    for i in range(0, len(list_data)):
        print('i-----', i)
        a = face_recognition.load_image_file('./pic/' + list_data[i])
        e = face_recognition.face_encodings(a)
        if len(e) > 0:
            known_encodings.append(e[0])

    return known_encodings


def compare():
    # Load a test image and get encondings for it
    image_to_test = face_recognition.load_image_file("./unkonw/xiawu2.jpg")
    image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
    # See how far apart the test image is from the known faces
    known_encodings = load_data()
    face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
    for i, face_distance in enumerate(face_distances):
        print("The test image has a distance of {:.2} from known image #{}".format(face_distance, i))
        print(
            "- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6))
        print("- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(
            face_distance < 0.5))
        print()


if __name__ == '__main__':
    print(compare())
